package com.sbhotel.chotellauncher.adapter;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.constant.Constants;

import java.util.ArrayList;
import java.util.List;

public class WifiScanAdapter extends BaseAdapter {

    List<ScanResult> list = new ArrayList<>();
    Context mContext;
    WifiManager manager;
    WiFiItemClickListner clickListner;

    public WifiScanAdapter(Context context) {
        mContext = context;
        manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_wifi, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.ssid.setText(list.get(position).SSID);
        Log.d(Constants.LOG_TAG, "list.get(position).capabilities = " + list.get(position).capabilities);
        if (!list.get(position).capabilities.equals("[ESS]")) {
            viewHolder.imgStatus.setImageResource(R.mipmap.icon_wifi_authentication);
        } else {
            viewHolder.imgStatus.setVisibility(View.INVISIBLE);
        }

        List<WifiConfiguration> configs = manager.getConfiguredNetworks();
        String mConnectedAccessPoint = "";
        if (configs != null) {
            for (WifiConfiguration config : configs) {
                if (config.status == WifiConfiguration.Status.CURRENT) {
                    mConnectedAccessPoint = config.SSID.replaceAll("\"", "");
                    Log.d(Constants.LOG_TAG, "mConnectedAccessPoint = " + mConnectedAccessPoint);
                }
            }
        }
        if (list.get(position).SSID.equals(mConnectedAccessPoint)) {
            viewHolder.imgStatus.setVisibility(View.VISIBLE);
            viewHolder.imgStatus.setImageResource(R.mipmap.icon_wifi_certified);
        }

        int levelValue = list.get(position).level;
        //根据获得的信号强度发送信度
        if (levelValue <= 0 && levelValue >= -50) {
            viewHolder.signal.setImageResource(R.mipmap.icon_wifi_signal);
        } else if (levelValue < -50 && levelValue >= -80) {
            viewHolder.signal.setImageResource(R.mipmap.icon_wifi_signal);
        } else {
            viewHolder.signal.setImageResource(R.mipmap.icon_wifi_signal);
        }
        //viewHolder.imgStatus.setImageResource(R.mipmap.icon_wifi_authentication);
        //viewHolder.signal.setImageResource(R.mipmap.icon_wifi_signal);
        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("leihuiming", "onClick");
                if(clickListner != null) {
                    clickListner.onItemClick(position);
                }
            }
        });
        return view;
    }

    public void setList(List<ScanResult> results) {
        sortByLevel(results);
        list = results;
        notifyDataSetChanged();
    }

    public void setClickListner(WiFiItemClickListner listner) {
        clickListner = listner;
    }

    class ViewHolder {
        ImageView imgStatus;
        TextView ssid;
        ImageView signal;
        View item;

        public ViewHolder(View view) {
            imgStatus = view.findViewById(R.id.iv_wifi_status);
            ssid = view.findViewById(R.id.tv_wifi_name);
            signal = view.findViewById(R.id.iv_wifi_signal);
            item = view.findViewById(R.id.item_wifi_layout);
        }
    }

    private void sortByLevel(List<ScanResult> list) {
        for (int i = 0; i < list.size() - 1; i++)
            for (int j = 1; j < list.size() - i - 1; j++) {
                if (list.get(j).level < list.get(j + 1).level)    //level属性即为强度
                {
                    int temp = list.get(j).level;
                    list.get(j).level = list.get(j + 1).level;
                    list.get(j + 1).level = temp;
                }
            }
    }

    public interface WiFiItemClickListner {
        public void onItemClick(int position);
    }
}
